package com.tanhua.manage.job;

import com.tanhua.domain.mongo.UserFreeze;
import com.tanhua.dubbo.api.UserFreezeApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 定时任务类
 */
@Component
@Slf4j
public class UserFreezeJob {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference
    private UserFreezeApi userFreezeApi;

    @Scheduled(cron = "0 0/30 0/1 1/1 * ?")
    public void login() {
        log.info("冻结登录方法开始");
        publicMethod();
        log.info("冻结登录方法结束");
    }

    @Scheduled(cron = "0 0/30 0/1 1/1 * ?")
    public void comment() {
        System.out.println("冻结评论方法开始");
        publicMethod();
        System.out.println("冻结评论方法结束");

    }

    @Scheduled(cron = "0 0/30 0/1 1/1 * ?")
    public void saveMovement() {
        System.out.println("冻结发布动态方法开始");
        publicMethod();
        System.out.println("冻结发布动态方法结束");
    }

    public void publicMethod() {
        List<UserFreeze> userFreezeList = userFreezeApi.findAll();
        for (UserFreeze userFreeze : userFreezeList) {
            Integer userId = userFreeze.getUserId();
            if (redisTemplate.opsForValue().get("userFreezeId_" + userId + "_time") == null) {
                userFreeze.setFreezingTime(0);
                userFreeze.setFreezingRange(0);
                userFreezeApi.update(userFreeze);
            }
        }
    }


}
